//
//  SheetViewController.m
//  NBBankEnt
//
//  Created by KK on 14-8-11.
//
//

#import "SheetViewController.h"
#import "AppDelegate.h"
#import "JMEnv.h"
#define isIOS7 ( [[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=7 )
#define isIOS8 ( [[[[UIDevice currentDevice] systemVersion] substringToIndex:1] intValue]>=8 )
@interface SheetViewController ()

@end

@implementation SheetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    
    self.view.backgroundColor = [UIColor clearColor];
    self.view.superview.backgroundColor = [UIColor clearColor];
    if(!_arrayPickView){
        self.arrayPickView = @[@"1",@"2",@"3"];
    }
    
    //确定、取消按钮
    UIBarButtonItem *buttonItemFlexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *buttonItemRight = [[UIBarButtonItem alloc] initWithTitle:@"确定"
                                                                style: UIBarButtonItemStyleBordered
                                                                target:self
                                                                action:@selector(selectAffirm)];
    
    UIBarButtonItem *buttonItemLeft = [[UIBarButtonItem alloc] initWithTitle:@"取消"
                                                                style:UIBarButtonItemStyleBordered
                                                                target:self
                                                                action:@selector(selectCanceled)];
    UIBarButtonItem *buttonItemDelete  = [[UIBarButtonItem alloc] initWithTitle:@"删除"
                                                                  style:UIBarButtonItemStyleBordered
                                                                  target:self
                                                                  action:@selector(selectDelete)];
    
    UIToolbar *tool=[[UIToolbar alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height-216.0-44.0, 320, 44)];
    tool.barStyle=UIBarStyleDefault;
    if (isIOS7)
    {
        tool.frame = CGRectMake(0, self.view.bounds.size.height-216.0-44.0, 320, 44) ;
        [tool setBarTintColor:ENV.iToolBarColor];
        tool.barStyle=UIBarStyleBlackOpaque;
        [tool setTintColor:[UIColor whiteColor]];
    }
    
    tool.backgroundColor=[UIColor clearColor];
    
    NSArray *array = nil;
    if (_pickType == JMPickerLogin) {
        array = [[NSArray alloc] initWithObjects:buttonItemLeft,buttonItemDelete,buttonItemFlexible,buttonItemRight,nil];
    }else{
        array = [[NSArray alloc] initWithObjects:buttonItemLeft,buttonItemFlexible,buttonItemRight,nil];
    }
    [tool setItems: array];
    
    [self.view addSubview:tool];
    
    
    switch (_pickType) {
        case JMPickerNormal:
            //选择
            _pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-216.0, 320, 216.0)];
            _pickView.delegate = self;
            _pickView.dataSource = self;
            _pickView.showsSelectionIndicator = YES;
            _pickView.backgroundColor = [UIColor whiteColor];
            [self.view addSubview:_pickView];
            break;
        case JMPickerDatePicker:
            _datePickView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-216.0, 320, 216.0)];
            _datePickView.datePickerMode=UIDatePickerModeDate;
            _datePickView.backgroundColor = [UIColor whiteColor];
            [self.view addSubview:_datePickView];
            break;
        case JMPickerLogin:
            //选择
            _pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-216.0, 320, 216.0)];
            _pickView.delegate = self;
            _pickView.dataSource = self;
            _pickView.showsSelectionIndicator = YES;
            _pickView.backgroundColor = [UIColor whiteColor];
            [self.view addSubview:_pickView];
        default:
            break;
    }
    
    
    
    
    _titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-216.0-44.0, 320, 44)];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.font = [UIFont boldSystemFontOfSize:19];
    _titleLabel.textAlignment = UITextAlignmentCenter;
    _titleLabel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_titleLabel];
}

-(void)viewWillAppear:(BOOL)animated
{
    _selectRow = 0;
    if (_pickView) {
        NSLog(@"come here");
        if([_arrayPickView count] > 0){
            [self pickerView:_pickView didSelectRow:_selectRow inComponent:0];
        }
        [_pickView selectRow:_selectRow inComponent:0 animated:YES];
        [_pickView reloadAllComponents];
    }
    _titleLabel.text = _titleText;
    [super viewWillAppear:animated];
}

#pragma mark pickerView  methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if ([self.arrayPickView count]==0)
        pickerView.userInteractionEnabled=NO;
    else
        pickerView.userInteractionEnabled=YES;
    return [self.arrayPickView count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (self.arrayPickView.count>row)
        return [self.arrayPickView objectAtIndex: row];
    else
        return @"";
    
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _selectRow = row;
    [pickerView reloadAllComponents];
    //NSLog(@"%@",self.arrayPickView);
    [self.YYSelString setString: [self.arrayPickView objectAtIndex: row]];
    NSLog(@"%@",self.YYSelString);
}

-(void)selectAffirm
{
    AppDelegate* del = (AppDelegate*)[UIApplication sharedApplication].delegate;
    
    UIViewController* presentingVC = del.window.rootViewController;
    if (presentingVC.presentedViewController) {
        presentingVC = presentingVC.presentedViewController;
    }
    [presentingVC dismissViewControllerAnimated:YES completion:^{
        NSLog(@"YYSelString is %@",self.YYSelString);
        switch (_pickType) {
            case JMPickerNormal:
                [self.iDelegate didSeleted:_selectRow];
                break;
            case JMPickerDatePicker:
            {
                NSDate* date = [_datePickView date];
                NSDateFormatter* format = [[NSDateFormatter alloc]init];
                [format setDateFormat:@"yyyy-MM-dd"];
                NSString* substring = [format stringFromDate:date];
                [_YYSelString setString:substring];
                [self.dateDelegate didSeletedDateStr:[_YYSelString copy]];
                break;
            }
            case JMPickerLogin:
                [self.iDelegate didSeleted:_selectRow];
                break;
            default:
                break;
        }
        if (_table) {
            [_table reloadData];
        }
    }];
}

-(void)selectCanceled
{
    _YYSelString = nil;
    AppDelegate* del = (AppDelegate*)[UIApplication sharedApplication].delegate;
//    UIViewController* rootVC = del.window.rootViewController;
    UIViewController* presentingVC = del.window.rootViewController;
    if (presentingVC.presentedViewController) {
        presentingVC = presentingVC.presentedViewController;
    }
    [presentingVC dismissViewControllerAnimated:YES completion:^{
        NSLog(@"YYSelString is %@",self.YYSelString);
        switch (_pickType) {
            case JMPickerNormal:
                if ([self.iDelegate respondsToSelector:@selector(didCancle)]) {
                    [self.iDelegate didCancle];
                }
                break;
            case JMPickerDatePicker:
            {
                NSLog(@"JMPickerDatePicker Cancle Do Nothing!");
                break;
            }
            default:
                break;
        }
    }];
}

-(void)selectDelete
{
    switch (_pickType) {
        case JMPickerNormal:
            NSLog(@"JMPickerDatePicker Cancle Do Nothing!");
            break;
        case JMPickerDatePicker:
            NSLog(@"JMPickerDatePicker Cancle Do Nothing!");
            break;
        case JMPickerLogin:
            if ([self.iDelegate respondsToSelector:@selector(didDelete:)]) {
                [self.iDelegate didDelete:_selectRow];
                [_pickView reloadAllComponents];
            }
            break;
        default:
            break;
    }
}


@end
